{% load helpers %}

<div class="card">
  <h5 class="card-header">Contacts</h5>
  <div class="card-body">
    {% with contacts=object.contacts.all %}
      {% if contacts.exists %}
        <table class="table table-hover">
          <tr>
            <th>Name</th>
            <th>Role</th>
            <th>Priority</th>
            <th>Phone</th>
            <th>Email</th>
            <th></th>
          </tr>
          {% for contact in contacts %}
            <tr>
              <td>{{ contact.contact|linkify }}</td>
              <td>{{ contact.role|placeholder }}</td>
              <td>{{ contact.get_priority_display|placeholder }}</td>
              <td>
                {% if contact.contact.phone %}
                  <a href="tel:{{ contact.contact.phone }}">{{ contact.contact.phone }}</a>
                {% else %}
                  {{ ''|placeholder }}
                {% endif %}
              </td>
              <td>
                {% if contact.contact.email %}
                  <a href="mailto:{{ contact.contact.email }}">{{ contact.contact.email }}</a>
                {% else %}
                  {{ ''|placeholder }}
                {% endif %}
              </td>
              <td class="text-end noprint">
                {% if perms.tenancy.change_contactassignment %}
                  <a href="{% url 'tenancy:contactassignment_edit' pk=contact.pk %}?return_url={{ object.get_absolute_url }}" class="btn btn-warning btn-sm lh-1" title="Edit">
                    <i class="mdi mdi-pencil" aria-hidden="true"></i>
                  </a>
                {% endif %}
                {% if perms.tenancy.delete_contactassignment %}
                  <a href="{% url 'tenancy:contactassignment_delete' pk=contact.pk %}?return_url={{ object.get_absolute_url }}" class="btn btn-danger btn-sm lh-1" title="Delete">
                    <i class="mdi mdi-trash-can-outline" aria-hidden="true"></i>
                  </a>
                {% endif %}
              </td>
            </tr>
          {% endfor %}
        </table>
      {% else %}
        <div class="text-muted">None</div>
      {% endif %}
    {% endwith %}
  </div>
  {% if perms.tenancy.add_contactassignment %}
    <div class="card-footer text-end noprint">
      <a href="{% url 'tenancy:contactassignment_add' %}?content_type={{ object|content_type_id }}&object_id={{ object.pk }}&return_url={{ object.get_absolute_url }}" class="btn btn-primary btn-sm">
        <i class="mdi mdi-plus-thick" aria-hidden="true"></i> Add a contact
      </a>
    </div>
  {% endif %}
</div>
